package com.itheima.aop;

import com.itheima.pojo.Goods;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Name ProcessDataAdvice
 * @Author BaiXin
 * @Date 2022-10-20 13:32
 */
@Component
@Aspect
public class ProcessDataAdvice {

    @Pointcut("execution(* com.itheima.service.impl.GoodsServiceImpl.add(..))")
    public void pt(){}

    @Around("pt()")
    public Object processData(ProceedingJoinPoint pjp) throws Throwable {
        //获取参数
        Object[] args = pjp.getArgs();
        System.out.println("1111");
        if (args != null && args.length>0) {
            for (int i = 0; i < args.length; i++) {
                //判断是否需要处理
                if (args[i] instanceof Goods) {
                    //处理参数
                   ((Goods) args[i]).setCreated(new Date());
                }
            }
        }
        //调用方法时，需要把处理后的参数给传递出去
        Object result = pjp.proceed(args);
        return result;

    }

}
